﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.APP.APP_UI.Delete_Form
{
    public partial class ICM_Master_resource_Type : System.Web.UI.Page
    {
        CommandControl cmdFac = new CommandControl();
        IBLLResourceMasterType bll = new ResourceMasterTypeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataGrid();
            }
        }

        public void LoadDataGrid()
        {
            IList<ResourceMasterTypeInfo> info = bll.GetResourceMasterType(SystemInfo._DeleteRecordStatus);
            Grid.DataSource = info;
            Grid.DataBind();

            cmdFac.GridCommand(panelList, panelGrid, panelCommand);
        }

        public void GetKeyValue(string seq)
        {
            if (seq.Length > 0)
            {
                ResourceMasterTypeInfo info = bll.GetResourceMasterType(Convert.ToInt32(seq));

                txtCode.Text = info.Code;
                txtName.Text = info.Name;
                txtDescr.Text = info.Descr;
                cmbStatus.SelectedValue = info.Depre_cal;
                txtUpdatedBy.Text = info.InfoRecord.Updated_by.ToString();
                txtUpdatedWhen.Text = info.InfoRecord.Updated_when.ToString();

                cmdFac.ViewCommand(panelList, panelGrid, panelCommand);
            }
        }

        protected void Restore_Click(object sender, EventArgs e)
        {
            IList<ResourceMasterTypeInfo> infoList = new List<ResourceMasterTypeInfo>();
            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
                {
                    ResourceMasterTypeInfo info = new ResourceMasterTypeInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                    infoRecord.Restored_by = 100;
                    infoRecord.Restored_when = DateTime.Now;
                    info.InfoRecord = infoRecord;
                    infoList.Add(info);
                }
            }
            else
            {
                ResourceMasterTypeInfo info = new ResourceMasterTypeInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(hdSeq.Value);
                infoRecord.Restored_by = 100;
                infoRecord.Restored_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._RestoreNull);
                return;
            }

            try
            {
                bll.Restore(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._RestoreError);
                return;
            }

            LoadDataGrid();
        }

        protected void DeleteFromDB_Click(object sender, EventArgs e)
        {
            IList<ResourceMasterTypeInfo> infoList = new List<ResourceMasterTypeInfo>();
            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
                {
                    ResourceMasterTypeInfo info = new ResourceMasterTypeInfo();
                    info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                    infoList.Add(info);
                }
            }
            else
            {
                ResourceMasterTypeInfo info = new ResourceMasterTypeInfo();
                info.Seq = Convert.ToInt32(hdSeq.Value);
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bll.DeleteFromDB(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadDataGrid();
        }

        protected void ShowGrid_Click(object sender, EventArgs e)
        {
            LoadDataGrid();
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadDataGrid();
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            GetKeyValue(e.Argument);
        }
    }
}
